<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 XML解析</h1>


        <h2>什么是XML？</h2>
        <p>XML 指可扩展标记语言（e<b>X</b>tensible <b>M</b>arkup <b>L</b>anguage），标准通用标记语言的子集，是一种用于标记电子文件使其具有结构性的标记语言。
            你可以通过本站学习<a href="/xml/xml-tutorial.html" target="_blank" title="XML教程">XML教程</a>
        </p>
        <p>XML 被设计用来传输和存储数据。</p>
        <p>XML是一套定义语义标记的规则，这些标记将文档分成许多部件并对这些部件加以标识。</p>
        <p>它也是元标记语言，即定义了用于定义其他与特定领域有关的、语义的、结构化的标记语言的句法语言。</p>

        <h2>python对XML的解析</h2>
        <p>常见的XML编程接口有DOM和SAX，这两种接口处理XML文件的方式不同，当然使用场合也不同。</p>
        <p>python有三种方法解析XML，SAX，DOM，以及ElementTree:</p>
        <h3>1.SAX (simple API for XML )</h3>
        <p>python 标准库包含SAX解析器，SAX用事件驱动模型，通过在解析XML的过程中触发一个个的事件并调用用户定义的回调函数来处理XML文件。</p>

        <h3>2.DOM(Document Object Model)</h3>
        <p>将XML数据在内存中解析成一个树，通过对树的操作来操作XML。</p>



        <p>本章节使用到的XML实例文件movies.xml内容如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="tag">&lt;collection</span><span class="pln"> </span><span class="atn">shelf</span><span class="pun">=</span><span class="atv">"New Arrivals"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;movie</span><span class="pln"> </span><span class="atn">title</span><span class="pun">=</span><span class="atv">"Enemy Behind"</span><span class="tag">&gt;</span><span class="pln">
   </span><span class="tag">&lt;type&gt;</span><span class="pln">War, Thriller</span><span class="tag">&lt;/type&gt;</span><span class="pln">
   </span><span class="tag">&lt;format&gt;</span><span class="pln">DVD</span><span class="tag">&lt;/format&gt;</span><span class="pln">
   </span><span class="tag">&lt;year&gt;</span><span class="pln">2003</span><span class="tag">&lt;/year&gt;</span><span class="pln">
   </span><span class="tag">&lt;rating&gt;</span><span class="pln">PG</span><span class="tag">&lt;/rating&gt;</span><span class="pln">
   </span><span class="tag">&lt;stars&gt;</span><span class="pln">10</span><span class="tag">&lt;/stars&gt;</span><span class="pln">
   </span><span class="tag">&lt;description&gt;</span><span class="pln">Talk about a US-Japan war</span><span class="tag">&lt;/description&gt;</span><span class="pln">
</span><span class="tag">&lt;/movie&gt;</span><span class="pln">
</span><span class="tag">&lt;movie</span><span class="pln"> </span><span class="atn">title</span><span class="pun">=</span><span class="atv">"Transformers"</span><span class="tag">&gt;</span><span class="pln">
   </span><span class="tag">&lt;type&gt;</span><span class="pln">Anime, Science Fiction</span><span class="tag">&lt;/type&gt;</span><span class="pln">
   </span><span class="tag">&lt;format&gt;</span><span class="pln">DVD</span><span class="tag">&lt;/format&gt;</span><span class="pln">
   </span><span class="tag">&lt;year&gt;</span><span class="pln">1989</span><span class="tag">&lt;/year&gt;</span><span class="pln">
   </span><span class="tag">&lt;rating&gt;</span><span class="pln">R</span><span class="tag">&lt;/rating&gt;</span><span class="pln">
   </span><span class="tag">&lt;stars&gt;</span><span class="pln">8</span><span class="tag">&lt;/stars&gt;</span><span class="pln">
   </span><span class="tag">&lt;description&gt;</span><span class="pln">A schientific fiction</span><span class="tag">&lt;/description&gt;</span><span class="pln">
</span><span class="tag">&lt;/movie&gt;</span><span class="pln">
   </span><span class="tag">&lt;movie</span><span class="pln"> </span><span class="atn">title</span><span class="pun">=</span><span class="atv">"Trigun"</span><span class="tag">&gt;</span><span class="pln">
   </span><span class="tag">&lt;type&gt;</span><span class="pln">Anime, Action</span><span class="tag">&lt;/type&gt;</span><span class="pln">
   </span><span class="tag">&lt;format&gt;</span><span class="pln">DVD</span><span class="tag">&lt;/format&gt;</span><span class="pln">
   </span><span class="tag">&lt;episodes&gt;</span><span class="pln">4</span><span class="tag">&lt;/episodes&gt;</span><span class="pln">
   </span><span class="tag">&lt;rating&gt;</span><span class="pln">PG</span><span class="tag">&lt;/rating&gt;</span><span class="pln">
   </span><span class="tag">&lt;stars&gt;</span><span class="pln">10</span><span class="tag">&lt;/stars&gt;</span><span class="pln">
   </span><span class="tag">&lt;description&gt;</span><span class="pln">Vash the Stampede!</span><span class="tag">&lt;/description&gt;</span><span class="pln">
</span><span class="tag">&lt;/movie&gt;</span><span class="pln">
</span><span class="tag">&lt;movie</span><span class="pln"> </span><span class="atn">title</span><span class="pun">=</span><span class="atv">"Ishtar"</span><span class="tag">&gt;</span><span class="pln">
   </span><span class="tag">&lt;type&gt;</span><span class="pln">Comedy</span><span class="tag">&lt;/type&gt;</span><span class="pln">
   </span><span class="tag">&lt;format&gt;</span><span class="pln">VHS</span><span class="tag">&lt;/format&gt;</span><span class="pln">
   </span><span class="tag">&lt;rating&gt;</span><span class="pln">PG</span><span class="tag">&lt;/rating&gt;</span><span class="pln">
   </span><span class="tag">&lt;stars&gt;</span><span class="pln">2</span><span class="tag">&lt;/stars&gt;</span><span class="pln">
   </span><span class="tag">&lt;description&gt;</span><span class="pln">Viewable boredom</span><span class="tag">&lt;/description&gt;</span><span class="pln">
</span><span class="tag">&lt;/movie&gt;</span><span class="pln">
</span><span class="tag">&lt;/collection&gt;</span></pre>

        <h2>python使用SAX解析xml</h2>
        <p>SAX是一种基于事件驱动的API。</p>
        <p>利用SAX解析XML文档牵涉到两个部分:解析器和事件处理器。</p>
        <p>解析器负责读取XML文档,并向事件处理器发送事件,如元素开始跟元素结束事件;</p>
        <p>而事件处理器则负责对事件作出相应,对传递的XML数据进行处理。
        </p>
        <psax适于处理下面的问题：< p="">
        <ul>
            <li>
                1、对大型文件进行处理；</li><li>
            2、只需要文件的部分内容，或者只需从文件中得到特定信息。</li><li>
            3、想建立自己的对象模型的时候。</li></ul>
        <p>在python中使用sax方式处理xml要先引入xml.sax中的parse函数，还有xml.sax.handler中的ContentHandler。</p>
        <h3>ContentHandler类方法介绍</h3>
        <p><strong>characters(content)方法</strong></p>
        <p>调用时机：</p>
        <p>从行开始，遇到标签之前，存在字符，content的值为这些字符串。</p>
        <p>从一个标签，遇到下一个标签之前， 存在字符，content的值为这些字符串。</p>
        <p>从一个标签，遇到行结束符之前，存在字符，content的值为这些字符串。</p>
        <p>标签可以是开始标签，也可以是结束标签。</p>


        <p><strong>startDocument()方法</strong></p>
        <p>文档启动的时候调用。</p>


        <p><strong>endDocument()方法</strong></p>
        <p>解析器到达文档结尾时调用。</p>


        <p><strong>startElement(name, attrs)方法</strong></p>
        <p>遇到XML开始标签时调用，name是标签的名字，attrs是标签的属性值字典。</p>


        <p><strong>endElement(name)方法</strong></p>
        <p>遇到XML结束标签时调用。 </p>

        <h2>make_parser方法</h2>
        <p>以下方法创建一个新的解析器对象并返回。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">xml</span><span class="pun">.</span><span class="pln">sax</span><span class="pun">.</span><span class="pln">make_parser</span><span class="pun">(</span><span class="pln"> </span><span class="pun">[</span><span class="pln">parser_list</span><span class="pun">]</span><span class="pln"> </span><span class="pun">)</span></pre>
        <p>参数说明:</p>
        <ul>
            <li><strong>parser_list</strong> -
                可选参数，解析器列表</li>
        </ul>

        <h2>parser方法</h2>
        <p>以下方法创建一个 SAX 解析器并解析xml文档：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">xml</span><span class="pun">.</span><span class="pln">sax</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln"> xmlfile</span><span class="pun">,</span><span class="pln"> contenthandler</span><span class="pun">[,</span><span class="pln"> errorhandler</span><span class="pun">])</span></pre>
        <p>参数说明:</p>
        <ul>
            <li><strong>xmlfile</strong> -
                xml文件名</li>
            <li><strong>contenthandler</strong> -
                必须是一个ContentHandler的对象</li>
            <li><strong>errorhandler</strong> -
                如果指定该参数，errorhandler必须是一个SAX ErrorHandler对象</li>
        </ul>

        <h2>parseString方法</h2>
        <p>parseString方法创建一个XML解析器并解析xml字符串：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">xml</span><span class="pun">.</span><span class="pln">sax</span><span class="pun">.</span><span class="pln">parseString</span><span class="pun">(</span><span class="pln">xmlstring</span><span class="pun">,</span><span class="pln"> contenthandler</span><span class="pun">[,</span><span class="pln"> errorhandler</span><span class="pun">])</span></pre>
        <p>参数说明:</p>
        <ul>
            <li><strong>xmlstring</strong> -
                xml字符串</li>
            <li><strong>contenthandler</strong> -
                必须是一个ContentHandler的对象</li>
            <li><strong>errorhandler</strong> -
                如果指定该参数，errorhandler必须是一个SAX ErrorHandler对象</li>
        </ul>

        <h2>Python 解析XML实例</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> xml</span><span class="pun">.</span><span class="pln">sax

</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MovieHandler</span><span class="pun">(</span><span class="pln"> xml</span><span class="pun">.</span><span class="pln">sax</span><span class="pun">.</span><span class="typ">ContentHandler</span><span class="pln"> </span><span class="pun">):</span><span class="pln">
   </span><span class="kwd">def</span><span class="pln"> __init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">):</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">type </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">format </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">year </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">rating </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">stars </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">description </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">

   </span><span class="com"># 元素开始调用</span><span class="pln">
   </span><span class="kwd">def</span><span class="pln"> startElement</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> tag</span><span class="pun">,</span><span class="pln"> attributes</span><span class="pun">):</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> tag
      </span><span class="kwd">if</span><span class="pln"> tag </span><span class="pun">==</span><span class="pln"> </span><span class="str">"movie"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"*****Movie*****"</span><span class="pun">)</span><span class="pln">
         title </span><span class="pun">=</span><span class="pln"> attributes</span><span class="pun">[</span><span class="str">"title"</span><span class="pun">]</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Title:"</span><span class="pun">,</span><span class="pln"> title</span><span class="pun">)</span><span class="pln">

   </span><span class="com"># 元素结束调用</span><span class="pln">
   </span><span class="kwd">def</span><span class="pln"> endElement</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> tag</span><span class="pun">):</span><span class="pln">
      </span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Type:"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">type</span><span class="pun">)</span><span class="pln">
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"format"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Format:"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">format</span><span class="pun">)</span><span class="pln">
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"year"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Year:"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">year</span><span class="pun">)</span><span class="pln">
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"rating"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Rating:"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">rating</span><span class="pun">)</span><span class="pln">
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"stars"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Stars:"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">stars</span><span class="pun">)</span><span class="pln">
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"description"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Description:"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">description</span><span class="pun">)</span><span class="pln">
      </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pln">

   </span><span class="com"># 读取字符时调用</span><span class="pln">
   </span><span class="kwd">def</span><span class="pln"> characters</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> content</span><span class="pun">):</span><span class="pln">
      </span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">type </span><span class="pun">=</span><span class="pln"> content
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"format"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">format </span><span class="pun">=</span><span class="pln"> content
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"year"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">year </span><span class="pun">=</span><span class="pln"> content
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"rating"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">rating </span><span class="pun">=</span><span class="pln"> content
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"stars"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">stars </span><span class="pun">=</span><span class="pln"> content
      </span><span class="kwd">elif</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="typ">CurrentData</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="str">"description"</span><span class="pun">:</span><span class="pln">
         </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">description </span><span class="pun">=</span><span class="pln"> content

</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> __name__ </span><span class="pun">==</span><span class="pln"> </span><span class="str">"__main__"</span><span class="pun">):</span><span class="pln">

   </span><span class="com"># 创建一个 XMLReader</span><span class="pln">
   parser </span><span class="pun">=</span><span class="pln"> xml</span><span class="pun">.</span><span class="pln">sax</span><span class="pun">.</span><span class="pln">make_parser</span><span class="pun">()</span><span class="pln">
   </span><span class="com"># turn off namepsaces</span><span class="pln">
   parser</span><span class="pun">.</span><span class="pln">setFeature</span><span class="pun">(</span><span class="pln">xml</span><span class="pun">.</span><span class="pln">sax</span><span class="pun">.</span><span class="pln">handler</span><span class="pun">.</span><span class="pln">feature_namespaces</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span><span class="pln">

   </span><span class="com"># 重写 ContextHandler</span><span class="pln">
   </span><span class="typ">Handler</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="typ">MovieHandler</span><span class="pun">()</span><span class="pln">
   parser</span><span class="pun">.</span><span class="pln">setContentHandler</span><span class="pun">(</span><span class="pln"> </span><span class="typ">Handler</span><span class="pln"> </span><span class="pun">)</span><span class="pln">

   parser</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="str">"movies.xml"</span><span class="pun">)</span></pre>
        <p>以上代码执行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Enemy</span><span class="pln"> </span><span class="typ">Behind</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">War</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Thriller</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> DVD
</span><span class="typ">Year</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2003</span><span class="pln">
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> PG
</span><span class="typ">Stars</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pln">
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Talk</span><span class="pln"> about a US</span><span class="pun">-</span><span class="typ">Japan</span><span class="pln"> war
</span><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Transformers</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Anime</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Science</span><span class="pln"> </span><span class="typ">Fiction</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> DVD
</span><span class="typ">Year</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1989</span><span class="pln">
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> R
</span><span class="typ">Stars</span><span class="pun">:</span><span class="pln"> </span><span class="lit">8</span><span class="pln">
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> A schientific fiction
</span><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Trigun</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Anime</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Action</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> DVD
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> PG
</span><span class="typ">Stars</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pln">
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Vash</span><span class="pln"> the </span><span class="typ">Stampede</span><span class="pun">!</span><span class="pln">
</span><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Ishtar</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Comedy</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> VHS
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> PG
</span><span class="typ">Stars</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span><span class="pln">
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Viewable</span><span class="pln"> boredom</span></pre>
        <p>完整的 SAX API 文档请查阅<a href="http://docs.python.org/library/xml.sax.html" target="_blank" rel="nofollow">Python SAX APIs</a></p>

        <h2>使用xml.dom解析xml</h2>
        <p>文件对象模型（Document Object Model，简称DOM），是W3C组织推荐的处理可扩展置标语言的标准编程接口。</p>
        <p>一个 DOM 的解析器在解析一个 XML 文档时，一次性读取整个文档，把文档中所有元素保存在内存中的一个树结构里，之后你可以利用DOM 提供的不同的函数来读取或修改文档的内容和结构，也可以把修改过的内容写入xml文件。
        </p>
        <p> python中用xml.dom.minidom来解析xml文件，实例如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">from</span><span class="pln"> xml</span><span class="pun">.</span><span class="pln">dom</span><span class="pun">.</span><span class="pln">minidom </span><span class="kwd">import</span><span class="pln"> parse
</span><span class="kwd">import</span><span class="pln"> xml</span><span class="pun">.</span><span class="pln">dom</span><span class="pun">.</span><span class="pln">minidom

</span><span class="com"># 使用minidom解析器打开 XML 文档</span><span class="pln">
</span><span class="typ">DOMTree</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> xml</span><span class="pun">.</span><span class="pln">dom</span><span class="pun">.</span><span class="pln">minidom</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="str">"movies.xml"</span><span class="pun">)</span><span class="pln">
collection </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DOMTree</span><span class="pun">.</span><span class="pln">documentElement
</span><span class="kwd">if</span><span class="pln"> collection</span><span class="pun">.</span><span class="pln">hasAttribute</span><span class="pun">(</span><span class="str">"shelf"</span><span class="pun">):</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Root element : %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> collection</span><span class="pun">.</span><span class="pln">getAttribute</span><span class="pun">(</span><span class="str">"shelf"</span><span class="pun">))</span><span class="pln">

</span><span class="com"># 在集合中获取所有电影</span><span class="pln">
movies </span><span class="pun">=</span><span class="pln"> collection</span><span class="pun">.</span><span class="pln">getElementsByTagName</span><span class="pun">(</span><span class="str">"movie"</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 打印每部电影的详细信息</span><span class="pln">
</span><span class="kwd">for</span><span class="pln"> movie </span><span class="kwd">in</span><span class="pln"> movies</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"*****Movie*****"</span><span class="pun">)</span><span class="pln">
   </span><span class="kwd">if</span><span class="pln"> movie</span><span class="pun">.</span><span class="pln">hasAttribute</span><span class="pun">(</span><span class="str">"title"</span><span class="pun">):</span><span class="pln">
      </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Title: %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> movie</span><span class="pun">.</span><span class="pln">getAttribute</span><span class="pun">(</span><span class="str">"title"</span><span class="pun">))</span><span class="pln">

   type </span><span class="pun">=</span><span class="pln"> movie</span><span class="pun">.</span><span class="pln">getElementsByTagName</span><span class="pun">(</span><span class="str">'type'</span><span class="pun">)[</span><span class="lit">0</span><span class="pun">]</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Type: %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> type</span><span class="pun">.</span><span class="pln">childNodes</span><span class="pun">[</span><span class="lit">0</span><span class="pun">].</span><span class="pln">data</span><span class="pun">)</span><span class="pln">
   format </span><span class="pun">=</span><span class="pln"> movie</span><span class="pun">.</span><span class="pln">getElementsByTagName</span><span class="pun">(</span><span class="str">'format'</span><span class="pun">)[</span><span class="lit">0</span><span class="pun">]</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Format: %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> format</span><span class="pun">.</span><span class="pln">childNodes</span><span class="pun">[</span><span class="lit">0</span><span class="pun">].</span><span class="pln">data</span><span class="pun">)</span><span class="pln">
   rating </span><span class="pun">=</span><span class="pln"> movie</span><span class="pun">.</span><span class="pln">getElementsByTagName</span><span class="pun">(</span><span class="str">'rating'</span><span class="pun">)[</span><span class="lit">0</span><span class="pun">]</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Rating: %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> rating</span><span class="pun">.</span><span class="pln">childNodes</span><span class="pun">[</span><span class="lit">0</span><span class="pun">].</span><span class="pln">data</span><span class="pun">)</span><span class="pln">
   description </span><span class="pun">=</span><span class="pln"> movie</span><span class="pun">.</span><span class="pln">getElementsByTagName</span><span class="pun">(</span><span class="str">'description'</span><span class="pun">)[</span><span class="lit">0</span><span class="pun">]</span><span class="pln">
   </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Description: %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> description</span><span class="pun">.</span><span class="pln">childNodes</span><span class="pun">[</span><span class="lit">0</span><span class="pun">].</span><span class="pln">data</span><span class="pun">)</span></pre>
        <p>以上程序执行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Root</span><span class="pln"> element </span><span class="pun">:</span><span class="pln"> </span><span class="typ">New</span><span class="pln"> </span><span class="typ">Arrivals</span><span class="pln">
</span><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Enemy</span><span class="pln"> </span><span class="typ">Behind</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">War</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Thriller</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> DVD
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> PG
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Talk</span><span class="pln"> about a US</span><span class="pun">-</span><span class="typ">Japan</span><span class="pln"> war
</span><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Transformers</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Anime</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Science</span><span class="pln"> </span><span class="typ">Fiction</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> DVD
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> R
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> A schientific fiction
</span><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Trigun</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Anime</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Action</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> DVD
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> PG
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Vash</span><span class="pln"> the </span><span class="typ">Stampede</span><span class="pun">!</span><span class="pln">
</span><span class="pun">*****</span><span class="typ">Movie</span><span class="pun">*****</span><span class="pln">
</span><span class="typ">Title</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Ishtar</span><span class="pln">
</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Comedy</span><span class="pln">
</span><span class="typ">Format</span><span class="pun">:</span><span class="pln"> VHS
</span><span class="typ">Rating</span><span class="pun">:</span><span class="pln"> PG
</span><span class="typ">Description</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Viewable</span><span class="pln"> boredom</span></pre>
        <p>完整的  DOM API 文档请查阅<a href="http://docs.python.org/library/xml.dom.html" target="_blank" rel="nofollow">Python DOM APIs</a>。</p>

        </psax适于处理下面的问题：<></div>
</div>
</body>
</html>
